package com.ynunicom.dc.dingdingcontractapp.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.ynunicom.dc.dingdingcontractapp.entity.requestbodyentity.NewStaffRequestBody;
import com.ynunicom.dc.dingdingcontractapp.entity.requestbodyentity.PageRequestEntity;
import com.ynunicom.dc.dingdingcontractapp.entity.requestbodyentity.TransBody;
import com.ynunicom.dc.dingdingcontractapp.entity.requestbodyentity.UpdateStaffRequestBody;
import com.ynunicom.dc.dingdingcontractapp.service.UserOptionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @author: jinye.Bai
 * @date: 2020/5/25 14:18
 */
@Service
@Slf4j
public class UserOptionServiceImpl implements UserOptionService {

    @Resource
    RestTemplate restTemplate;

    private static final String CREATE_URL = "https://oapi.dingtalk.com/user/create?access_token={access_token}";

    private static final String UPDATE_URL = "https://oapi.dingtalk.com/user/update?access_token={access_token}";

    private static final String DELETE_URL = "https://oapi.dingtalk.com/user/delete?access_token={access_token}&userid={userid}";

    private static final String USERINFO_URL = "https://oapi.dingtalk.com/user/get?access_token={access_token}&userid={userid}";

    private static final String DEPT_USERID_URL = "https://oapi.dingtalk.com/user/getDeptMember?access_token={access_token}&deptId={deptId}";

    private static final String DEPT_USERINFO_URL = "https://oapi.dingtalk.com/user/simplelist?access_token={access_token}&department_id={department_id}";

    private static final String DEPT_USERDETAIL_PAGE_URL = "https://oapi.dingtalk.com/user/listbypage?access_token={access_token}&department_id={department_id}&offset={offset}&size={size}&order={order}";

    private static final String UNIONID_2_USERID_URL = "https://oapi.dingtalk.com/user/getUseridByUnionid?access_token={access_token}&unionid={unionid}";

    private static final String MOBILE_2_USERID_URL = "https://oapi.dingtalk.com/user/get_by_mobile?access_token={access_token}&mobile={mobile}";

    private static final String UPPER_DEPT_LIST_URL = "https://oapi.dingtalk.com/department/list_parent_depts?access_token={access_token}&userId={userId}";

    @Override
    public JSONObject createNewStaff(TransBody<NewStaffRequestBody> transBody) {
        Map<String,String> urlVar = new HashMap<>(1);
        urlVar.put("access_token",transBody.getAccess_token());
        JSONObject jsonObject = restTemplate.postForObject(CREATE_URL,transBody.getRequestBody(),JSONObject.class,urlVar);
        if(!"0".equals(jsonObject.getString("errcode"))){
            log.info(String.format("员工:[%s]增加失败",transBody.getRequestBody().getName()));
        }
        else {
            log.info(String.format("员工:[%s]被增加了",transBody.getRequestBody().getName()));
        }
        return jsonObject;
    }

    @Override
    public JSONObject updateStaff(TransBody<UpdateStaffRequestBody> transBody) {
        Map<String,String> urlVar = new HashMap<>(1);
        urlVar.put("access_token",transBody.getAccess_token());
        JSONObject jsonObject = restTemplate.postForObject(UPDATE_URL,transBody.getRequestBody(),JSONObject.class,urlVar);
        if(!"0".equals(jsonObject.getString("errcode"))){
            log.info(String.format("员工:[%s]更新失败",transBody.getRequestBody().getUserid()));
        }
        else {
            log.info(String.format("员工:[%s]被更新了",transBody.getRequestBody().getUserid()));
        }
        return jsonObject;
    }

    @Override
    public JSONObject deleteStaff(TransBody<String> transBody) {
        Map<String,String> urlVar = new HashMap<>(2);
        urlVar.put("access_token",transBody.getAccess_token());
        urlVar.put("userid",transBody.getRequestBody());
        JSONObject jsonObject = restTemplate.getForObject(DELETE_URL,JSONObject.class,urlVar);
        if(!"0".equals(jsonObject.getString("errcode"))){
            log.info(String.format("员工:[%s]删除失败",transBody.getRequestBody()));
        }
        else {
            log.info(String.format("员工:[%s]被删除了",transBody.getRequestBody()));
        }
        return jsonObject;
    }

    @Override
    public JSONObject getUserInfo(TransBody<String> transBody) {
        Map<String,String> urlVar = new HashMap<>(2);
        urlVar.put("access_token",transBody.getAccess_token());
        urlVar.put("userid",transBody.getRequestBody());
        return restTemplate.getForObject(USERINFO_URL,JSONObject.class,urlVar);
    }

    @Override
    public JSONObject getDeptUserId(TransBody<String> transBody) {
        Map<String,String> urlVar = new HashMap<>(2);
        urlVar.put("deptId",transBody.getRequestBody());
        urlVar.put("access_token",transBody.getAccess_token());
        return restTemplate.getForObject(DEPT_USERID_URL,JSONObject.class,urlVar);
    }

    @Override
    public JSONObject getDeptUserInfo(TransBody<String> transBody) {
        Map<String,String> urlVar = new HashMap<>(2);
        urlVar.put("department_id",transBody.getRequestBody());
        urlVar.put("access_token",transBody.getAccess_token());
        return restTemplate.getForObject(DEPT_USERINFO_URL,JSONObject.class,urlVar);
    }

    @Override
    public JSONObject getDeptUserDetail(TransBody<PageRequestEntity> transBody,String department_id) {
        Map<String,String> urlVar = new HashMap<>(5);
        urlVar.put("department_id",department_id);
        urlVar.put("access_token",transBody.getAccess_token());
        urlVar.put("offset",String.valueOf(transBody.getRequestBody().getOffset()));
        urlVar.put("size",String.valueOf(transBody.getRequestBody().getSize()));
        if(transBody.getRequestBody().getOrder()!=null&&!"".equals(transBody.getRequestBody().getOrder())){
            urlVar.put("order",transBody.getRequestBody().getOrder());
        }
        else{
            urlVar.put("order","entry_desc");
        }
        return restTemplate.getForObject(DEPT_USERDETAIL_PAGE_URL,JSONObject.class,urlVar);
    }

    @Override
    public JSONObject exchangeUnionId2UserId(TransBody<String> transBody) {
        Map<String,String> urlVar = new HashMap<>(2);
        urlVar.put("unionid",transBody.getRequestBody());
        urlVar.put("access_token",transBody.getAccess_token());
        return restTemplate.getForObject(UNIONID_2_USERID_URL,JSONObject.class,urlVar);
    }

    @Override
    public JSONObject mobileToUserId(TransBody<String> transBody) {
        Map<String,String> urlVar = new HashMap<>(2);
        urlVar.put("mobile",transBody.getRequestBody());
        urlVar.put("access_token",transBody.getAccess_token());
        return restTemplate.getForObject(MOBILE_2_USERID_URL,JSONObject.class,urlVar);
    }

    @Override
    public JSONObject allUpperDeptPath(TransBody<String> transBody) {
        Map<String,String> urlVar = new HashMap<>(2);
        urlVar.put("userId",transBody.getRequestBody());
        urlVar.put("access_token",transBody.getAccess_token());
        return restTemplate.getForObject(UPPER_DEPT_LIST_URL,JSONObject.class,urlVar);
    }


}
